import fetch from './fetch'

export default {

  saveToken (token) {
    return localStorage.setItem('token', token)
  },

  getToken () {
    return localStorage.getItem('token')
  },

  saveData (key, data) {
    return localStorage.setItem(key, JSON.stringify(data))
  },

  getData (key) {
    return JSON.parse(localStorage.getItem(key))
  },

  removeData (key) {
    return localStorage.removeItem(key)
  },

  saveUser (user) {
    this.saveData('currentUser', user)
  },

  getUser () {
    return this.getData('currentUser')
  },

  callUserInfo (callback) {
    fetch.post('/user/info', {}, (d) => {
      this.saveUser(d)
      callback && callback()
    })
  },

  login (token, callBack) {
    this.saveToken(token)
    // this.callUserInfo(callBack)
  },

  logout () {
    this.removeData('token')
    // this.removeData('currentUser')
  },
}
